﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Windows.Forms;

using Noris.Schedule.Support;
using Noris.Schedule.Support.Core;
using Noris.Schedule.Support.Services;
using Noris.Schedule.Support.Data;
using Noris.Schedule.Support.Sql;
using Noris.Schedule.Planning.ProcessData;
using Noris.Schedule.Planning.DataFace;

namespace Noris.Schedule.Planning.Services
{
    public class PlanCombin : IFunctionMenuItem
    {
        bool IFunctionMenuItem.IsFunctionSuitableFor(FunctionMenuItemSuitableArgs args)
        {
            bool result;

            result = (args.KeyGraphMode == RowGraphMode.TaskCapacityLink && args.KeyAreaType == FunctionMenuItemAreaType.GraphElement && args.KeyElementClassNumber == Noris.Schedule.Planning.ProcessData.Constants.ClassNumberWork);
            // result = false;


            if (result)
            {
                args.MenuCaption = "test zaplánování kombinace";
                args.MenuToolTipText = "test zaplánování kombinace";
            }
            return result;
        }

        bool IFunctionMenuItem.IsMenuItemEnabledFor(Noris.Schedule.Support.Services.FunctionMenuItemRunArgs args)
        {
            bool result;

            result = true;
            return result;
        }

        void IFunctionMenuItem.Run(FunctionMenuItemRunArgs args)
        {
            PlanningDataCls planningData;
            if (!PlanningDataCls.TryGetPlanningData(args.DataSource, out planningData)) return;

            // Najdu datum příjmu tasku, a posunu jej o 1 až dva dny dopředu:
            double addDay = 1D;
            CapacityPlanWorkItemCls task = planningData.FindTaskC(args.ClickedItem);
            DateTime target = task.TimeWork.End.AddDays(addDay);

            List<DataPointerStr> splitItems = this._SplitItems(args, args.ClickedItem, 4M, 6M);

            this._MoveItemsToTime(args, splitItems, target);
        }
        private List<DataPointerStr> _SplitItems(FunctionMenuItemRunArgs args, DataPointerStr clickedItem, decimal qty1, decimal qty2)
        {
            PlanningDataCls planningData;
            if (!PlanningDataCls.TryGetPlanningData(args.DataSource, out planningData)) return null;

            // CapacityPlanWorkItemCls task = planningData.FindTaskC(clickedItem);
            // var axis = task.AxisData;

            PlanningInteractiveSplitAxisSArgs splitArgs = new PlanningInteractiveSplitAxisSArgs();
            splitArgs.SplitItemSource = clickedItem;
            splitArgs.SplitItemList.Add(new PlanningInteractiveSplitAxisSItemArgs(qty1, TimeRange.TimeDirection.ToFuture, 1));
            splitArgs.SplitItemList.Add(new PlanningInteractiveSplitAxisSItemArgs(qty2, TimeRange.TimeDirection.ToFuture, 2));

            planningData.InteractiveSplitAxisS(splitArgs);

            splitArgs.ResultCopyTo(args);
            
            List<DataPointerStr> result = new List<DataPointerStr>();
            foreach (var si in splitArgs.SplitItemList)
            {
                if (si.Tag != null && (int)si.Tag > 0)
                    result.AddRange(si.ResultElementPointerList);
            }
            return result;
        }
        private void _MoveItemsToTime(FunctionMenuItemRunArgs args, List<DataPointerStr> splitItems, DateTime target)
        {
            PlanningDataCls planningData;
            if (!PlanningDataCls.TryGetPlanningData(args.DataSource, out planningData)) return;

            PlanningInteractiveMoveArgs moveArgs = new PlanningInteractiveMoveArgs();
            moveArgs.CapacityLimitForActiveTask = LimitedCType.Unlimited;
            moveArgs.SetFixedTask = true;
            moveArgs.PullAdjacentForActiveTask = true;
            moveArgs.PullAdjacentForActiveTree = true;

            foreach (DataPointerStr splitItem in splitItems)
            {
                CapacityPlanWorkItemCls task = planningData.FindTaskC(splitItem);
                moveArgs.MoveTaskVectorDict.Add(task.TaskGID, new TimeVector(target, TimeRange.TimeDirection.ToFuture));
            }

            planningData.InteractiveMove(moveArgs);

            moveArgs.ResultCopyTo(args);
        }
    }
}